package util;

import java.awt.Color;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Locale;
import java.util.ResourceBundle;

/**
 * @author Jonas Dietz and Ramon Victor from the Cologne University of Applied Sciences
 * @since 2012
 * This Class saves the attributes for jim.
 */
public class Attributes 
{
    public static ResourceBundle rs = ResourceBundle.getBundle("language.Multilang", Locale.ENGLISH);
    public static Color backgroundColor = new Color (46, 54, 52);
    public static Color textColor = new Color (255, 112, 1);
    public static Color warningColor = Color.RED;
    private static String fileName = "Preferences.jimc";

    /**
     * This method loads the user specified attributes
     * If the attributes are not specified, it will write the standard attributes to the Preferences.jimc file
     */
    public static void loadAttributes(){
	int i = 0;
	Color tC, bgC;
	int[] tCC = new int[3];
	int[] bgCC = new int[3];

	try{
	    BufferedReader in = new BufferedReader(new FileReader(fileName));
	    String line = null;

	    while((line = in.readLine()) != null){
		switch (i)
		{
		case 0: tCC[0] = Integer.parseInt(line); i++; break;
		case 1: tCC[1] = Integer.parseInt(line); i++; break;
		case 2: tCC[2] = Integer.parseInt(line); i++; break;
		case 3: bgCC[0] = Integer.parseInt(line); i++; break;
		case 4: bgCC[1] = Integer.parseInt(line); i++; break;
		case 5: bgCC[2] = Integer.parseInt(line); i++; break;
		}
	    }

	    in.close();

	    tC = new Color(tCC[0], tCC[1], tCC[2]);
	    bgC = new Color(bgCC[0], bgCC[1], bgCC[2]);
	    textColor = tC;
	    backgroundColor = bgC;
	} catch(FileNotFoundException e){
	    FileWriter writer;
	    File file;

	    try{
		file = new File(fileName);
		writer = new FileWriter(file);
		writer.write("" + textColor.getRed());
		writer.write(System.getProperty("line.separator"));
		writer.write("" + textColor.getGreen());
		writer.write(System.getProperty("line.separator"));
		writer.write("" + textColor.getBlue());
		writer.write(System.getProperty("line.separator"));
		writer.write("" + backgroundColor.getRed());
		writer.write(System.getProperty("line.separator"));
		writer.write("" + backgroundColor.getGreen());
		writer.write(System.getProperty("line.separator"));
		writer.write("" + backgroundColor.getBlue());
		writer.flush();
		writer.close();
	    } catch(IOException e1){
		e1.printStackTrace();
	    }
	} catch(IOException e){
	    e.printStackTrace();
	}
    }

    /**
     * This Method is used to save the users preferences
     * @param tC chosen textColor
     * @param bgC chosen backgroundColor
     */
    public static void saveAttributes(Color tC, Color bgC){
	FileWriter writer;
	File file;

	try{
	    file = new File(fileName);
	    writer = new FileWriter(file);
	    writer.write("" + tC.getRed());
	    writer.write(System.getProperty("line.separator"));
	    writer.write("" + tC.getGreen());
	    writer.write(System.getProperty("line.separator"));
	    writer.write("" + tC.getBlue());
	    writer.write(System.getProperty("line.separator"));
	    writer.write("" + bgC.getRed());
	    writer.write(System.getProperty("line.separator"));
	    writer.write("" + bgC.getGreen());
	    writer.write(System.getProperty("line.separator"));
	    writer.write("" + bgC.getBlue());
	    writer.flush();
	    writer.close();
	} catch(IOException e){
	    e.printStackTrace();
	}
    }
}
